package com.yf.mediaserver.util;

import java.util.concurrent.*;

/**
 * @name: MediaConst
 * @author: leihuangyan
 * @classPath: com.yf.media.consts.MediaConst
 * @date: 2023/3/24
 * @description:
 */
public class MediaThread {

    /**
     * 自定义链式线程池
     */
    public static ThreadPoolExecutor THREAD_POOL = new ThreadPoolExecutor(12, 12, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10),new MyThreadFactory("exec"), new ThreadPoolExecutor.CallerRunsPolicy());

    public static ThreadPoolExecutor MEDIA_THREAD_POOL_READ = new ThreadPoolExecutor(12, 12, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10240),new MyThreadFactory("read"), new ThreadPoolExecutor.CallerRunsPolicy());

    public static ThreadPoolExecutor MEDIA_THREAD_POOL_WRITE = new ThreadPoolExecutor(8, 8, 60, TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(10),new MyThreadFactory("write"), new ThreadPoolExecutor.CallerRunsPolicy());
    public static  ScheduledExecutorService  SCHEDULED_EXECUTOR_SERVICE = new ScheduledThreadPoolExecutor(8, new MyThreadFactory("task"));



   private static class  MyThreadFactory implements ThreadFactory {

       private int counter = 0;
       private final String name;

       public MyThreadFactory(String name) {
           this.name = name;
       }

       @Override
       public Thread newThread(Runnable r) {
           Thread t = new Thread(r);
           t.setName(String.format("Media-Thread-%s-%d",name,counter++));
           t.setPriority(Thread.NORM_PRIORITY);
           t.setDaemon(false);
           return t;
       }
    }

}
